
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>1.6 标记 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="替换与正则表达式编译.html" />
    
    
    <link rel="prev" href="先行断言.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    第一章：正则表达式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="正则表达式入门.html">
            
                <a href="正则表达式入门.html">
            
                    
                    1.1 正则表达式入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Python中的正则表达式.html">
            
                <a href="Python中的正则表达式.html">
            
                    
                    1.2 Python中的正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="基本正则表达式.html">
            
                <a href="基本正则表达式.html">
            
                    
                    1.3 基本正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="分组.html">
            
                <a href="分组.html">
            
                    
                    1.4 分组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="先行断言.html">
            
                <a href="先行断言.html">
            
                    
                    1.5 先行断言
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.6" data-path="标记.html">
            
                <a href="标记.html">
            
                    
                    1.6 标记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="替换与正则表达式编译.html">
            
                <a href="替换与正则表达式编译.html">
            
                    
                    1.7 替换与正则编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="正则表达式练习.html">
            
                <a href="正则表达式练习.html">
            
                    
                    1.8 正则表达式练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Python爬虫学习目标.html">
            
                <a href="Python爬虫学习目标.html">
            
                    
                    第二章：爬虫与Requests
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Requests的简单使用.html">
            
                <a href="Requests的简单使用.html">
            
                    
                    2.1 Requests的简单使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="深入了解Requests.html">
            
                <a href="深入了解Requests.html">
            
                    
                    2.2 深入了解Requets
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="结构化与非结构化.html">
            
                <a href="结构化与非结构化.html">
            
                    
                    第三章：非结构化与结构化数据提取
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="正则抓取.html">
            
                <a href="正则抓取.html">
            
                    
                    3.1 使用正则的爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="xpath与XML.html">
            
                <a href="xpath与XML.html">
            
                    
                    3.2 Xpath与XML类库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="lxml.html">
            
                <a href="lxml.html">
            
                    
                    3.3 LXML库的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Xpath爬虫案例.html">
            
                <a href="Xpath爬虫案例.html">
            
                    
                    3.4 案例：贴吧图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Json与JsonPath.html">
            
                <a href="Json与JsonPath.html">
            
                    
                    3.5 Json与Json数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="实例：糗事百科网站抓取.html">
            
                <a href="实例：糗事百科网站抓取.html">
            
                    
                    3.6 糗事百科网站爬虫
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >1.6 标记</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6807;&#x8BB0;">&#x6807;&#x8BB0;</h2>
<h3 id="&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;">&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;</h3>
<p>&#x6700;&#x7B80;&#x5355;&#x76F4;&#x63A5;&#x7684;&#x6807;&#x8BB0;&#x662F; <code>re.IGNORECASE</code>&#xFF0C;&#x4ED6;&#x4F1A;&#x5BFC;&#x81F4;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x53D8;&#x4E3A;&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x3002;</p>
<pre><code class="lang-python">a = re.search(<span class="hljs-string">r&apos;python&apos;</span>,<span class="hljs-string">&quot;I LIKE PYTHON&quot;</span>,re.IGNORECASE)

b = re.search(<span class="hljs-string">r&apos;python&apos;</span>,<span class="hljs-string">&apos;I LIKE PYTHON&apos;</span>,re.I)

print(a)
print(b)
</code></pre>
<pre><code>&lt;re.Match object; span=(7, 13), match=&apos;PYTHON&apos;&gt;
&lt;re.Match object; span=(7, 13), match=&apos;PYTHON&apos;&gt;
</code></pre><h3 id="&#x70B9;&#x5339;&#x914D;&#x6362;&#x884C;&#x7B26;">&#x70B9;&#x5339;&#x914D;&#x6362;&#x884C;&#x7B26;</h3>
<p><code>re.DOTALL</code>&#x6807;&#x8BB0;&#xFF08;&#x522B;&#x540D;&#x4E3A;<code>re.S</code>&#xFF09;&#x53EF;&#x4EE5;&#x8BA9; <code>.</code>&#x5B57;&#x7B26;&#x9664;&#x4E86;&#x5339;&#x914D;&#x5176;&#x4ED6;&#x5B57;&#x7B26;&#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x5339;&#x914D;&#x6362;&#x884C;&#x7B26;&#x3002;</p>
<pre><code class="lang-python">a = re.search(<span class="hljs-string">r&apos;.+&apos;</span>,<span class="hljs-string">&apos;hello\nworld&apos;</span>)
b = re.search(<span class="hljs-string">r&apos;.+&apos;</span>,<span class="hljs-string">&apos;hello\nworld&apos;</span>,re.S)
c = re.search(<span class="hljs-string">r&apos;.+&apos;</span>,<span class="hljs-string">&apos;hello\nworld&apos;</span>,re.DOTALL)

print(a)
print(b)
print(c)
</code></pre>
<pre><code>&lt;re.Match object; span=(0, 5), match=&apos;hello&apos;&gt;
&lt;re.Match object; span=(0, 11), match=&apos;hello\nworld&apos;&gt;
&lt;re.Match object; span=(0, 11), match=&apos;hello\nworld&apos;&gt;
</code></pre><h3 id="&#x591A;&#x884C;&#x6A21;&#x5F0F;">&#x591A;&#x884C;&#x6A21;&#x5F0F;</h3>
<p><code>re.MULTILINE</code>&#x6807;&#x8BB0;&#xFF08;&#x522B;&#x540D;&#x4E3A;<code>re.M</code>&#xFF09;&#x5BFC;&#x81F4;&#x4EC5;&#x80FD;&#x591F;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;&#x5F00;&#x59CB;&#x4E0E;&#x7ED3;&#x675F;&#x7684;<code>^</code>&#x4E0E;<code>$</code>&#x5B57;&#x7B26;&#x53EF;&#x4EE5;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;&#x5185;&#x4EFB;&#x610F;&#x884C;&#x7684;&#x5F00;&#x59CB;&#x4E0E;&#x7ED3;&#x675F;</p>
<pre><code class="lang-python">a = re.search(<span class="hljs-string">r&apos;^bar&apos;</span>,<span class="hljs-string">&apos;foo\nbar&apos;</span>)
b = re.search(<span class="hljs-string">r&apos;^bar&apos;</span>,<span class="hljs-string">&apos;foo\nbar&apos;</span>,re.M)

print(a)
print(b)
</code></pre>
<pre><code>None
&lt;re.Match object; span=(4, 7), match=&apos;bar&apos;&gt;
</code></pre><h3 id="&#x8BE6;&#x7EC6;&#x6A21;&#x5F0F;">&#x8BE6;&#x7EC6;&#x6A21;&#x5F0F;</h3>
<p><code>re.VERBOSE</code>&#x6807;&#x8BB0;(&#x522B;&#x540D;&#x4E3A;<code>re.X</code>)&#x5141;&#x8BB8;&#x590D;&#x6742;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4EE5;&#x66F4;&#x5BB9;&#x6613;&#x7684;&#x65B9;&#x5F0F;&#x8868;&#x793A;&#x3002;</p>
<p>&#x8BE5;&#x6807;&#x8BB0;&#x505A;&#x4E24;&#x4EF6;&#x4E8B;&#xFF0C;&#x9996;&#x5148;&#xFF0C;&#x4ED6;&#x5BFC;&#x81F4;&#x6240;&#x6709;&#x7684;&#x7A7A;&#x767D;&#xFF08;&#x9664;&#x4E86;&#x5B57;&#x7B26;&#x7EC4;&#x4E2D;&#xFF09;&#x88AB;&#x5FFD;&#x7565;&#xFF0C;&#x5305;&#x62EC;&#x6362;&#x884C;&#x7B26;&#x3002;&#x5176;&#x6B21;&#xFF0C;&#x5B83;&#x5C06;<code>#</code>&#x5B57;&#x7B26;&#xFF08;&#x540C;&#x6837;&#xFF0C;&#x9664;&#x975E;&#x5728;&#x5B57;&#x7B26;&#x7EC4;&#x5185;&#xFF09;&#x5F53;&#x505A;&#x6CE8;&#x91CA;&#x5B57;&#x7B26;&#x3002;</p>
<pre><code class="lang-python">a = re.search(<span class="hljs-string">r&apos;(?P&lt;first&gt;[\d]{3})-(?P&lt;second&gt;[\d]{4})&apos;</span>,<span class="hljs-string">&apos;867-5556&apos;</span>)
b = re.search(<span class="hljs-string">r&quot;&quot;&quot;(?P&lt;first&gt;[\d]{3})
            -    #&#x5339;&#x914D;&#x4E00;&#x4E2A;  - &#x8FDE;&#x63A5;&#x7B26;
            (?P&lt;second&gt;[\d]{4})  # &#x5339;&#x914D;&#x56DB;&#x4E2A;&#x6570;&#x5B57;
             &quot;&quot;&quot;</span>,
             <span class="hljs-string">&apos;879-5578&apos;</span>,re.X)

print(a)
print(b)
</code></pre>
<pre><code>&lt;re.Match object; span=(0, 8), match=&apos;867-5556&apos;&gt;
&lt;re.Match object; span=(0, 8), match=&apos;879-5578&apos;&gt;
</code></pre><h3 id="&#x8C03;&#x8BD5;&#x6A21;&#x5F0F;">&#x8C03;&#x8BD5;&#x6A21;&#x5F0F;</h3>
<p><code>re.DEBUG</code> &#x6807;&#x8BB0;&#xFF08;&#x6CA1;&#x6709;&#x522B;&#x540D;&#xFF09;&#x5728;&#x7F16;&#x8BD1;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#x5C06;&#x4E00;&#x4E9B;&#x8C03;&#x8BD5;&#x4FE1;&#x606F;&#x8F93;&#x51FA;&#x5230;<code>sys.stderr</code></p>
<pre><code class="lang-python">a = re.search(<span class="hljs-string">r&apos;(?P&lt;first&gt;[\d]{3})-(?P&lt;second&gt;[\d]{4})&apos;</span>,<span class="hljs-string">&apos;867-5556&apos;</span>,re.DEBUG)

print(a)
</code></pre>
<pre><code>SUBPATTERN 1 0 0
  MAX_REPEAT 3 3
    IN
      CATEGORY CATEGORY_DIGIT
LITERAL 45
SUBPATTERN 2 0 0
  MAX_REPEAT 4 4
    IN
      CATEGORY CATEGORY_DIGIT

 0. INFO 4 0b0 8 8 (to 5)
 5: MARK 0
 7. REPEAT_ONE 9 3 3 (to 17)
11.   IN 4 (to 16)
13.     CATEGORY UNI_DIGIT
15.     FAILURE
16:   SUCCESS
17: MARK 1
19. LITERAL 0x2d (&apos;-&apos;)
21. MARK 2
23. REPEAT_ONE 9 4 4 (to 33)
27.   IN 4 (to 32)
29.     CATEGORY UNI_DIGIT
31.     FAILURE
32:   SUCCESS
33: MARK 3
35. SUCCESS
&lt;re.Match object; span=(0, 8), match=&apos;867-5556&apos;&gt;
</code></pre><h3 id="&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x6807;&#x8BB0;">&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x6807;&#x8BB0;</h3>
<p>&#x53EF;&#x80FD;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x6807;&#x8BB0;&#xFF0C;&#x4E3A;&#x4E86;&#x5B8C;&#x6210;&#x8FD9;&#x70B9;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>|</code>&#x64CD;&#x4F5C;&#x7B26;&#x3002;&#x6B63;&#x786E;&#x7684;&#x8BED;&#x6CD5;&#x662F;&#x4F8B;&#x5982;&#xFF1A;<code>re.DOTALL|re.MULTILINE</code> &#x6216; <code>re.S | re.M</code>&#x3002;</p>
<h3 id="&#x5185;&#x8054;&#x6807;&#x8BB0;">&#x5185;&#x8054;&#x6807;&#x8BB0;</h3>
<p>&#x6807;&#x8BB0;&#x4E5F;&#x53EF;&#x4EE5;&#x7B80;&#x5199;&#xFF0C;&#x5728;&#x4E00;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x91CC;&#x4F7F;&#x7528;&#x6807;&#x8BB0;&#x4E5F;&#x662F;&#x53EF;&#x80FD;&#x7684;&#xFF1A;</p>
<pre><code class="lang-python">a = re.search(<span class="hljs-string">&apos;(?i)FOO&apos;</span>,<span class="hljs-string">&apos;foo&apos;</span>)
print(a)
</code></pre>
<pre><code>&lt;re.Match object; span=(0, 3), match=&apos;foo&apos;&gt;
</code></pre><p>&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x8FD9;&#x91CC;&#x7684;<code>(?i)</code>&#xFF0C;&#x5C31;&#x7B49;&#x540C;&#x4E8E;&#x4F7F;&#x7528;re.IGNORECASE &#x6807;&#x8BB0;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="先行断言.html" class="navigation navigation-prev " aria-label="Previous page: 1.5 先行断言">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="替换与正则表达式编译.html" class="navigation navigation-next " aria-label="Next page: 1.7 替换与正则编译">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"1.6 标记","level":"1.2.6","depth":2,"next":{"title":"1.7 替换与正则编译","level":"1.2.7","depth":2,"path":"notes/替换与正则表达式编译.md","ref":"notes/替换与正则表达式编译.md","articles":[]},"previous":{"title":"1.5 先行断言","level":"1.2.5","depth":2,"path":"notes/先行断言.md","ref":"notes/先行断言.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"notes/标记.md","mtime":"2019-04-28T15:04:08.006Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-08T02:50:45.307Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

